package com.jcfk.eam.controller.main;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.eam.domain.query.main.EamWorkOrderRecordQuery;
import com.jcfk.eam.domain.dto.main.EamWorkOrderRecordDTO;
import com.jcfk.eam.service.main.EamWorkOrderRecordService;

import java.util.List;

/**
 * 作业工单处理记录
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-09-04
 */
@Api(tags = {"作业工单处理记录管理"})
@RestController
@RequestMapping("eamworkorderrecord")
public class EamWorkOrderRecordController {
    @Autowired
    private EamWorkOrderRecordService eamWorkOrderRecordService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamWorkOrderRecordQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询作业工单处理记录")
    @PostMapping("/queryByPage")
    public RetPage<EamWorkOrderRecordDTO> queryByPage(@RequestBody ReqPageParam<EamWorkOrderRecordQuery> requestParam) {
        DataPage<EamWorkOrderRecordDTO> page = new DataPage<EamWorkOrderRecordDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamWorkOrderRecordDTO.class);
        page = eamWorkOrderRecordService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  作业工单处理记录
     * 根据id 查询详情
     */
    @ApiOperation("查询作业工单处理记录")
    @GetMapping("/get/{id}")
    public RetResult<EamWorkOrderRecordDTO> get(@PathVariable("id") String id) {
            EamWorkOrderRecordDTO item = eamWorkOrderRecordService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理作业工单处理记录
     */
    @ApiOperation("保存作业工单处理记录")
    @PostMapping("/save")
    @RequestLogHeader(modular = "作业工单处理记录管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamWorkOrderRecordDTO eamWorkOrderRecord) {
        // 数据校验
        RetResult ret = eamWorkOrderRecordService.isValid(eamWorkOrderRecord);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamWorkOrderRecord.getId())){
            // 新增
            eamWorkOrderRecordService.insert(eamWorkOrderRecord);
        } else{
            // 更新
            eamWorkOrderRecordService.update(eamWorkOrderRecord);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除作业工单处理记录
     */
    @ApiOperation("批量删除作业工单处理记录")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "作业工单处理记录管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamWorkOrderRecordDTO eamWorkOrderRecord) {
        return RetResult.ok(eamWorkOrderRecordService.removeBigBatch(eamWorkOrderRecord.getIds()));
    }





    /**
     *  删除作业工单处理记录
     */
    @ApiOperation("删除作业工单处理记录")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "作业工单处理记录管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamWorkOrderRecordService.deleteItem(id);
        return RetResult.ok();
    }




    /**
     *  根据工单id查询作业工单处理记录
     * @param orderId 工单id
     */
    @ApiOperation("根据工单id查询作业工单处理记录")
    @GetMapping("/getByOrderId/{orderId}")
    public RetResult<List<EamWorkOrderRecordDTO>> getByOrderId(@PathVariable("orderId") String orderId) {
        return RetResult.ok(eamWorkOrderRecordService.getByOrderId(orderId));
    }

}
